<?

if(!defined(SCOT_RDF_SERIALIZER)) {
	define('SCOT_RDF_SERIALIZER', true);

	class Serializer {
		var $scotModel = null;
		function Serializer($model) {
			if(SCOT_DEBUG) echo('Call : RDFSerializer->RDFSerializer()<br>');
			
			$this->scotModel = $model;
		}
		
		function serialize($path) {
			if(SCOT_DEBUG) echo('Call : RDFSerializer->serialize()<br>');
			
			//echo('<h3>serialize start</h3>');
			
			$rdf = '<?xml version="1.0" encoding="UTF-8" ?>'."\n";
//			$rdf .='<!-- Generated by the SCOT Exporter for the WordPress'."\n";
//			$rdf .='# http://scot-project.org !-->'."\n";
			$rdf .= '<rdf:RDF xml:base="http://scot-project.org/scot/ns#" '."\n";
			$rdf .= 'xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" '."\n";
			$rdf .= 'xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" '."\n";
			$rdf .= 'xmlns:dc="http://purl.org/dc/elements/1.1/" '."\n";
			$rdf .= 'xmlns:dcterms="http://purl.org/dc/terms/" '."\n";
			$rdf .= 'xmlns:foaf="http://xmlns.com/foaf/0.1/" '."\n";
			$rdf .= 'xmlns:skos="http://www.w3.org/2004/02/skos/core#" '."\n";
			$rdf .= 'xmlns:sioc="http://rdfs.org/sioc/ns#" '."\n";
			$rdf .= 'xmlns:scot="http://scot-project.org/scot/ns#">'."\n";

			$userList = $this->scotModel->getUserList();
			$userName = $this->scotModel->getInfo(SCOT_SOURCE_USER_NAME, $userList[0]->getId());
			$blogname = $this->scotModel->getInfo(SCOT_SOURCE_BLOG_NAME, $userList[0]->getId());
			$siteurl = $this->scotModel->getInfo(SCOT_SOURCE_SITE_URL, $userList[0]->getId());
			$tagcloudid = $this->scotModel->getInfo(SCOT_SOURCE_TAGCLOUD_ID, $userList[0]->getId());
//			echo($siteurl.'<br>');
			if(substr($siteurl, count($siteurl) - 2, 1) == '/') {
				$siteurl = substr($siteurl, 0, count($siteurl) - 2);
			}
//			echo($siteurl.'<br>');
			$baseuri = $siteurl;
			
			$rdf .= '<scot:Tagcloud rdf:about="'.$baseuri.'/tagcloud/'.$userList[0]->getId().'">'."\n";
			$rdf .=	'	<dc:title>'.$blogname.'</dc:title>'."\n";
			$rdf .=	'	<dc:description>A SCOT profile describes the structure and contents of a weblog in a machine readable form. For more information please refer to http://scot-project.org/.</dc:description>'."\n";
			$rdf .=	'	<scot:hasUsergroup>'."\n";
			$rdf .=	'		<sioc:Usergroup rdf:about="'.$baseuri.'/usergroup/'.$userList[0]->getId().'">'."\n";
			$rdf .=	'			<sioc:has_member rdf:resource="'.$baseuri.'/user/'.$userList[0]->getId().'" />'."\n";
//			$rdf .=	'				<sioc:User rdf:about="'.$siteurl.'/?author=1" rdfs:label="'.$userName.'">'."\n";
//			$rdf .=	'					<rdfs:seeAlso rdf:resource="'.$siteurl.'/index.php?sioc_type=user&amp;sioc_id=1"/>'."\n";
//			$rdf .=	'				</sioc:User>'."\n";
//			$rdf .=	'			</sioc:has_member>'."\n";
			$rdf .=	'		</sioc:Usergroup>'."\n";
			$rdf .=	'	</scot:hasUsergroup>'."\n";
//			$rdf .=	'	<dc:creater rdf:resource="'.$siteurl.$userList[0].'/user" />'."\n";
			$rdf .=	'	<dc:creator>'."\n";
			$rdf .=	'		<foaf:Person rdf:about="'.$baseuri.'/user/'.$userList[0]->getId().'">'."\n";
			$rdf .=	'			<foaf:name>'.$userName.'</foaf:name>'."\n";
			$rdf .=	'		</foaf:Person>'."\n";
			$rdf .=	'	</dc:creator>'."\n";
			$rdf .=	'	<dc:publisher rdf:resource="http://www.wordpress.org" />'."\n";
			$rdf .=	'	<scot:totalPosts>'.$this->scotModel->getTotalPosts().'</scot:totalPosts>'."\n";
			$rdf .=	'	<scot:totalTags>'.$this->scotModel->getTotalTags().'</scot:totalTags>'."\n";
			$rdf .=	'	<scot:totalCooccurrences>'.$this->scotModel->getTotalCooccurrences().'</scot:totalCooccurrences>'."\n";
			$rdf .=	'	<scot:totalTagFrequency>'.$this->scotModel->getTotalTagFrequency().'</scot:totalTagFrequency>'."\n";
			$rdf .=	'	<scot:totalCooccurFrequency>'.$this->scotModel->getTotalCooccurrenceFrequency().'</scot:totalCooccurFrequency>'."\n";
			
			$rdf .=	'	<dcterms:created>'.date('Y-m-d',time()).'T'.date('H:i:s',time()).'</dcterms:created>'."\n";
			//scot:totalCooccurFrequency
			//tag
			foreach($this->scotModel->getTagList() as $tag) {
				$rdf .= '	<scot:hasTag>'."\n";
				$rdf .= '		<scot:Tag rdf:about="'.$baseuri.'/tag/'.$tag->getTagToID().'">'."\n";
				$rdf .= '			<rdfs:seeAlso rdf:resource="'.$baseuri."/".SCOT_REL_PATH.'scot/tag.php?tag='.$tag->getTagToID().'"'."/>\n";
				
				$rdf .= '			<scot:name>'.$tag->getName().'</scot:name>'."\n";
				$rdf .= '			<scot:ownAFrequency>'.$tag->getAFrequency().'</scot:ownAFrequency>'."\n";
				$rdf .= '			<scot:ownRFrequency>'.sprintf("%1\$.2f", $tag->getRFrequency($this->scotModel->getTotalTagFrequency())).'</scot:ownRFrequency>'."\n";
				$rdf .= '			<scot:lastUsedDate>'.$tag->getLastUsedDate().'</scot:lastUsedDate>'."\n";
				foreach($tag->getnarrowerTagList() as $ntag) {
					$tl = $this->scotModel->getTagList();
					$ntag = $tl[$ntag];
					$rdf .= '			<skos:narrower rdf:resource="'.$baseuri.'/tag/'.$ntag->getTagToID().'"/>'."\n";
				}
				foreach($tag->getbroaderTagList() as $btag) {
					$tl = $this->scotModel->getTagList();
					$btag = $tl[$btag];
					$rdf .= '			<skos:broader rdf:resource="'.$baseuri.'/tag/'.$btag->getTagToID().'"/>'."\n";
				}
				foreach($tag->getCooccurrenceList() as $coocurrence) {
					$rdf .= '			<scot:cooccurWith rdf:nodeID="Cooccurrence_'.$coocurrence->getName().'"/>'."\n";
				}
				$rdf .= '		</scot:Tag>'."\n";
				$rdf .= '	</scot:hasTag>'."\n";
			}

			$rdf .= '</scot:Tagcloud>'."\n";
			
			// freq
			foreach($this->scotModel->getCooccurrenceList() as $coocurrence) {
				$rdf .= '<scot:Cooccurrence rdf:nodeID="Cooccurrence_'.$coocurrence->getName().'">'."\n";
				$rdf .= '	<scot:cooccurAFrequency>'.$coocurrence->getAFrequency().'</scot:cooccurAFrequency>'."\n";
				$rdf .= '	<scot:cooccurRFrequency>'.sprintf("%1\$.2f", $coocurrence->getRFrequency($this->scotModel->getTotalCooccurrenceFrequency())).'</scot:cooccurRFrequency>'."\n";
				foreach($coocurrence->getTagList() as $tag) {
					$rdf .= '	<scot:cooccurTag rdf:resource="'.$baseuri.'/tag/'.$tag->getTagToID().'"/>'."\n";
				}
					
				$rdf .= '</scot:Cooccurrence>'."\n";
			}
			
			
			$rdf .= '</rdf:RDF>'."\n";
			
			$fh = fopen($path, 'w');
			fwrite($fh, $rdf);
			fclose($fh);
			
			//echo('<h3>serialize end</h3>');
		}
	}
}
?>